package cn.tedu.game2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Timer;
import java.util.TimerTask;

//面板类型(玻璃)
public class BallPanel extends JPanel implements MouseMotionListener {
    Ball[] balls = new Ball[20];
    Brick[] bricks = new Brick[10];
    int boardX=600,boardY=750,boradWidth=500,boardHeight=10;

    public BallPanel(){
        for(int i=0;i<balls.length;i++){
            balls[i] = new Ball();
        }
        //创建若干砖块放入数组
        for (int i = 0;i < bricks.length/2; i++){
            bricks[i] = new Brick(i*100+i*10,0);
        }
        for (int i = 5;i < bricks.length; i++){
            bricks[i] = new Brick(i%5*100+i%5*10,100);
        }

        //对事件进行监听注册
        this.addMouseMotionListener(this);
    }

    //重写，覆盖
    @Override
    public void paint(Graphics g) {
        //调用一下父类中的paint方法
        super.paint(g);
        for(int i=0;i<balls.length;i++){
            g.setColor(balls[i].color);
            g.fillOval(balls[i].x,balls[i].y,balls[i].length,balls[i].length);
        }
        //画挡板
        g.setColor(Color.black);
        g.fillRect(boardX,boardY,boradWidth,boardHeight);
        //画砖块
        g.setColor(Color.pink);
        for (int i = 0; i< bricks.length; i++){
            g.fillRect(bricks[i].x,bricks[i].y,bricks[i].width,bricks[i].height);
        }
    }

    public void action(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                ballMove();
                repaint();
                //球撞到挡板
                boardHitBall();
                //小球打到砖块
                ballHitBrick();
            }
        },0,20);
    }

    //小球打到砖块
    private void ballHitBrick() {
        for (int i = 0; i < balls.length; i++){
            Ball ball = balls[i];
            for (int j = 0; j < bricks.length; j++){
                Brick brick = bricks[j];
                ball.hitBrick(brick);
            }
        }
    }

    private void boardHitBall() {
        for (int i=0; i<balls.length; i++){
            Ball ball = balls[i];
            //小球正下方的坐标
            int ballX = ball.x + ball.length/2;
            int ballY = ball.y + ball.length;
            //挡板四个角坐标
            int x1,x2,x3,x4,y1,y2,y3,y4;
            x1 = boardX;
            x2 = boardX+boradWidth;
            x3 = boardX+boradWidth;
            x4 = x2;
            y1 = boardY;
            y2 = boardY;
            y3 = y1+boradWidth;
            y4 = y1+boardHeight;

            if (ballX>=x1&&ballX<x2&&ballY>=y1&&ballY<=y4){
                if(ball.state==2){ball.state=1;}
                if(ball.state==3){ball.state=0;}
                ball.move();
            }
        }
    }

    public void ballMove(){
        for (int i = 0; i < balls.length; i++) {
            balls[i].move();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {

        boardX = e.getX()-boradWidth/2;
        boardY = e.getY();

    }
}
